package de.java.shortys.myhome.fuelmonitor.controller;

import static de.java.shortys.myhome.fuelmonitor.lang.LanguageConstants.FUELFILLING_ADD;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import de.java.shortys.myhome.api.controller.AbstractSwingModuleController;
import de.java.shortys.myhome.api.controller.AbstractSwingSubModuleController;
import de.java.shortys.myhome.api.controller.IController;
import de.java.shortys.myhome.api.controller.navigation.NavigationController;
import de.java.shortys.myhome.fuelmonitor.model.IFuelFilling;
import de.java.shortys.myhome.fuelmonitor.validation.FillingValidator;
import de.java.shortys.myhome.fuelmonitor.view.SubModuleViewFillingAdd;
import de.java.shortys.myhome.language.controller.LanguageManager;

public class SubModuleControllerFillingAdd extends AbstractSwingSubModuleController {

	public static final String ID = "FuelMonitor_FillingAdd";

	public SubModuleControllerFillingAdd(AbstractSwingModuleController root, IController parent) {
		super(root, parent, new SubModuleViewFillingAdd());
		initialize();
	}

	@Override
	public boolean allowActivate() {
		return true;
	}

	@Override
	public void collapse() {
		// TODO Auto-generated method stub

	}

	@Override
	public void expand() {
		// TODO Auto-generated method stub

	}

	@Override
	public String getIcon() {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public String getTitle() {
		return LanguageManager.getInstance(FuelMonitorController.NAME).get(FUELFILLING_ADD);
	}

	@Override
	public void initialize() {
		// Empty
		SubModuleViewFillingAdd view = (SubModuleViewFillingAdd) getView();
		initializeViewListeners(view);
	}

	private void initializeViewListeners(final SubModuleViewFillingAdd view) {
		view.addSaveListener(new ActionListener() {
			@Override
			public void actionPerformed(ActionEvent e) {
				actionSave(view);
			}
		});
		view.addCancelListener(new ActionListener() {
			@Override
			public void actionPerformed(ActionEvent e) {
				deactivate();
			}
		});
	}

	private void actionSave(SubModuleViewFillingAdd view) {
		final FillingValidator validator = new FillingValidator(view);
		if (validator.validate()) {
			FuelMonitorController controller = (FuelMonitorController) getParent();
			IFuelFilling object = validator.getObject();
			if (object != null) {
				controller.createFuelFilling(object);
				getView().clear();
				NavigationController.getInstance().jumpToModule(SubModuleControllerOverview.ID);
			}
		}
	}

	@Override
	public void onActivate() {
		// TODO Auto-generated method stub

	}

	@Override
	public void onDeactivate() {
		// TODO Auto-generated method stub

	}

	@Override
	public void onInitialize() {
		// TODO Auto-generated method stub
	}

	@Override
	public boolean list() {
		return true;
	}

	@Override
	public String getId() {
		return ID;
	}

}
